<template>
  <div id="home">
    <!--  -->
    <el-container class="home_container">
      <!-- 头部 -->
      <el-header>
        <div class="header_box">
          <img
            src="../assets/logo.png"
            alt=""
            srcset=""
            width="60px"
            height="60px"
          />
          <span>后台管理</span>
        </div>
        <el-button type="info" @click="logout()">退出</el-button>
      </el-header>

      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px':'200px'">
          <!-- 左侧菜单展开关闭按钮 -->
          <div class="toggle-button" @click="toggleCollaspe()">
            |||
          </div>
          <!-- 侧边栏菜单区域 -->
          <el-menu
            class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#369BFF"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            :default-active="activePath" 
            router
          >
          <!-- 只打开一个菜单 unique-opened -->
            <!-- 一级菜单 -->
            <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
              <!-- 一级菜单模板区 -->
              <template slot="title">
                <i :class="icon[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item  v-for="subItem in item.children" :key="subItem.id" :index="subItem.path" @click="saveNavState(subItem.path)">
                <template slot="title" >
                    <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  // 生命周期函数
  created(){
    this.getMenuList()
    this .activePath = window.sessionStorage.getItem('activePath')
  },
  data() {
    return {
      menuList:[],
      icon:{
        '125':'el-icon-user-solid',
        '103':'el-icon-s-tools',
        '101':'el-icon-s-goods',
        '102':'el-icon-s-order',
        '145':'el-icon-s-marketing',
      },
      // 是否折叠
      isCollapse:false,
      // 被激活的链接地址
      activePath:''
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message.success('退出成功')
    },
    handleOpen(key,keyPath){
      
    },
    handleClose(key,keyPath){

    },
    // 获取所有的菜单
    async getMenuList(){
      const {data:res} = await this.$http.get('/menus')
      // 获取到数据后赋值给menuList
      if(res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.menuList = res.data
    },
    toggleCollaspe(){
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活项
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath=activePath;
    }
  },
}
</script>
<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  // 左右对齐
  justify-content: space-between;
  padding-left: 0px;
  // 按钮中间对齐
  align-items: center;
  color: #fff;
  font-size: 20px;
  // 嵌套div样式
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.home_container {
  height: 900px;
}
.toggle-button{
  background-color: #475163;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  // 文字间距
  letter-spacing: 0.3em;
  // 小手
  cursor: pointer;
}
.el-menu{
  border-right: none;
}
</style>